﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HexEditor_;
namespace Translator
{
    public class Translator
    {
        public enum ErrorCode
        {
            NoError,
            BytesLenthNotEnough,
            UnsupportedType,
        }
        static DataTypeManager dtManager;
        public static ErrorCode eLastError;

        static Translator()
        {
            dtManager = new DataTypeManager();
            dtManager.Initialize();
        }
        public static bool IsValidTarget(string str)
        {
            return dtManager.IsExist(str);
        }
        public static string Translate(string strInput, string targetKeyName)
        {
            eLastError = ErrorCode.NoError;

            byte[] bytesInput = InputAdapter.StringToHexBytesArray(strInput);
            if (bytesInput != null)
            {
                StringBuilder strResult = new StringBuilder();
                int nOffset = 0;
                string outString;
                while (nOffset < bytesInput.Length)
                {
                    if (dtManager.GetDataType(targetKeyName).GetBytesRequired() >
                        bytesInput.Length - nOffset)
                    {
                        eLastError = ErrorCode.BytesLenthNotEnough;
                        break;
                    }
                    nOffset += dtManager.Parse(bytesInput, nOffset,
                        targetKeyName, out outString);

                    if (outString.Length > 0)
                    {
                        strResult.Append(" ");
                        strResult.Append(outString.ToCharArray());
                    }
                }
                return strResult.ToString();
            }
            else
            {
                return "";
            }
            
        }
    }
}
